<?php

namespace app\admin\controller;

use app\common\model\ExamAnswer;
use app\common\model\PaperQuestions;
use app\common\model\Questions;
use app\common\model\Exam as ExamModel;
use think\Request;

class Exam extends Base
{
    public function __construct()
    {
        parent::__construct();
//        $this->isLogin();
    }

    /**
     * @route('questions/questionlist')
     * 题目列表
     */
    public function questionlist()
    {
        $get['ques_name'] = input('get.ques_name');
        $questionModel = new Questions();
        $questions = $questionModel->get_list($get,'','',$this->limit);
//        var_dump($questions);die;
        $tvar = array(
            "title" => "题目列表",
            "time"  => time(),
            "data"  => $questions,
            "get"  => $get,
        );
        $page = $questions->render();
        $this->assign("data", $tvar);
        $this->assign('page', $page);
        return $this->fetch('questions/questionlist');
    }


    /**
     * @route('questions/boxquestion')
     * 编辑添加题目
     */
    public function boxquestion(Request $request)
    {
        $questionModel = new Questions();
        if(\request()->isAjax()){
            $param = $request->param();
//            var_dump($param);die;
            $data = [
                'question' => $param['question'],
                'option' => serialize($param['option']),
            ];
//            var_dump($param);die;
            // 处理
            $rs = $questionModel->edit($data,$param['ques_id']);
            if($rs === false){
                $this->error('操作失败');
            }
            $this->success('操作成功');
        }
        $id = $_GET['ques_id'];
        $question = null;
        $title = "添加题目";
        if($id != 0){
            $title = "编辑题目";
            $question = $questionModel->getOne(array('ques_id' => $id));
            if(!empty($question)){
                $question['option'] = unserialize($question['option']);
            }
        }
        $tvar = array(
            "title" => $title,
            "time"  => time(),
            "data" => $question,
            "get" => $id,
        );
//        var_dump($tvar);die;
        $this->assign("data", $tvar);
        return $this->fetch('questions/boxquestion');
    }

    protected function json($msg){
        $msg = mb_convert_encoding( $msg, 'UTF-8', 'UTF-8,GBK,GB2312,BIG5' );
        return $msg;
    }

    /**
     * @route('questions/delquestion')
     * 删除题目
     */
    public function delquestion()
    {
        $id = $_GET['ques_id'];
//        var_dump($id);die;
        $questionModel = new Questions();
        $rs = $questionModel->where('ques_id','=',$id)->delete();
        if($rs === false){
            $this->error("删除失败");
        }
        $this->success('删除成功 ');
    }

    /**
     * @route('exams/examlist')
     * 问卷列表
     */
    public function examlist()
    {
        $get['paper_title'] = input('get.paper_title');
        $examModel = new ExamModel();
        $exams = $examModel->get_list($get,'','',$this->limit);
//        var_dump($exams);die;
        $tvar = array(
            "title" => "问卷列表",
            "time"  => time(),
            "data"  => $exams,
            "get"  => $get,
        );
        $page = $exams->render();
        $this->assign("data", $tvar);
        $this->assign('page', $page);
        return $this->fetch('exam/examlist');
    }


    /**
     * @route('exams/boxexam')
     * 编辑添加问卷
     */
    public function boxexam(Request $request)
    {
        $examModel = new ExamModel();
        if(\request()->isAjax()){
            $param = $request->param();
//            var_dump($param);die;
            $data = [
                'paper_title' => $param['paper_title'],
            ];
            $exam = $examModel->getOne(array('is_publish' => 1));
            if(!empty($exam)){
                $this->error('已有发布问卷');
            }
//            var_dump($param);die;
            // 处理
            $rs = $examModel->edit($data,$param['paper_id']);
            if($rs === false){
                $this->error('操作失败');
            }
            $this->success('操作成功');
        }
        $id = $_GET['paper_id'];
        $exam = null;
        $title = "添加问卷";
        if($id != 0){
            $title = "编辑问卷";
            $exam = $examModel->getOne(array('paper_id' => $id));
        }
        $tvar = array(
            "title" => $title,
            "time"  => time(),
            "data" => $exam,
            "get" => $id,
        );
//        var_dump($tvar);die;
        $this->assign("data", $tvar);
        return $this->fetch('exam/boxexam');
    }

    /**
     * @route('exams/upanddown')
     * 操作问卷是否发布
     */
    public function UpAndDown()
    {
        $paper_id = input('paper_id');
        $examModel = new ExamModel();
        $where['paper_id'] = $paper_id;
        $exam = $examModel->where($where)->find();

        if(empty($exam)){
            $this->error('不存在该问卷');
        }
        if($exam['is_publish'] == 1){
            $state = 0;
        } else {
            $state = 1;
        }
        if($state == 1){
            $else_exam = $examModel->where('is_publish','=',1)->where('paper_id','<>',$paper_id)->find();
            if($else_exam){
                $this->error('已存在发布问卷');
            }
        }
        $param['is_publish'] = $state;
        $rs2 = $examModel->edit($param,$paper_id);
        if($rs2 === false){
            $this->error("操作失败");
        }
        $this->success('操作成功');
    }

    /**
     * @route('exams/check_questions')
     * 查看问卷题目
     */
    public function check_questions()
    {
        $paper_id = input('get.paper_id');
        $get['ques_name'] = input('get.ques_name');
        $get['paper_id'] = $paper_id;

        $examModel = new ExamModel();
        $exam = $examModel->where('paper_id','=',$paper_id)->find();

        if(empty($exam)){
            $this->error('不存在该问卷');
        }
        $questionModel = new Questions();
        $questions = $questionModel->get_list_by_paper($get,'','',$this->limit);
//        var_dump($questions);die;
        $tvar = array(
            "title" => "题目列表",
            "time"  => time(),
            "data"  => $questions,
            "get"  => $get,
        );
        $page = $questions->render();
        $this->assign("data", $tvar);
        $this->assign('page', $page);
        return $this->fetch('exam/questionlist');
    }

    /**
     * @route('exams/boxaddquestions')
     * 添加题目列表
     */
    public function boxaddquestions()
    {
        $paper_id = input('paper_id');
        $get['ques_name'] = input('get.ques_name');
        $get['paper_id'] = $paper_id;
//        var_dump($paper_id);
        $examModel = new ExamModel();
        $exam = $examModel->where('paper_id','=',$paper_id)->find();

        if(empty($exam)){
            $this->error('不存在该问卷');
        }
        if(\request()->isAjax()){
            $param = input('post.');
            if(empty($param['paper_id']) || empty($param['ques_id'])){
                $this->error('缺少关键参数');
            }
            $data = [
                'paper_id' => $param['paper_id'],
                'ques_id' => $param['ques_id'],
            ];
            $paperquesModel = new PaperQuestions();
            $rs = $paperquesModel->edit($data);
            if($rs === false){
                $this->error('操作失败');
            }
            $this->success('添加成功');
        }
        $questionModel = new Questions();
        $questions = $questionModel->get_list($get,'','',$this->limit);
//        var_dump($questions);die;
        $tvar = array(
            "title" => "题目列表",
            "time"  => time(),
            "data"  => $questions,
            "get"  => $get,
        );
        $page = $questions->render();
        $this->assign("data", $tvar);
        $this->assign('page', $page);
        return $this->fetch('exam/boxaddquestions');
    }

    /**
     * @route('exams/delexamquestion')
     * 关联删除问卷
     */
    public function delexamquestion()
    {
        $id = input('get.pq_id');
        $paperquesModel = new PaperQuestions();
        $rs = $paperquesModel->where('pq_id','=',$id)->delete();
        if($rs === false){
            $this->error("删除失败");
        }
        $this->success('删除成功 ');
    }

    /**
     * @route('exams/delexam')
     * 关联删除问卷
     */
    public function delexam()
    {
        $id = $_GET['paper_id'];
//        var_dump($id);die;
        $examModel = new ExamModel();
        $sql = "DELETE e,pq,s FROM ddt_exam_paper e LEFT JOIN ddt_exam_paperques pq ON e.paper_id = pq.paper_id LEFT JOIN ddt_exam_sheet s ON e.paper_id = s.at_id WHERE e.paper_id = {$id}";
        $rs = $examModel->query($sql);
        if($rs === false){
            $this->error("删除失败");
        }
        $this->success('删除成功 ');
    }

    /**
     * @route('exams/answererlist')
     * 查看问卷答题人
     */
    public function answererlist()
    {
        $paper_id = input('get.paper_id');
        $get['user_name'] = input('get.user_name');
        $get['paper_id'] = $paper_id;

        $examModel = new ExamModel();
        $exam = $examModel->where('paper_id','=',$paper_id)->find();

        if(empty($exam)){
            $this->error('不存在该问卷');
        }
        $answerModel = new ExamAnswer();
        $answerers = $answerModel->get_answerer_info($get,'u.user_id,u.nick_name,u.avatar,s.sheet_id,s.add_time,s.at_id','s.add_time desc','s.user_id',$this->limit);
//        echo "<pre>";
//        var_dump($answerers->toArray());die;
        $tvar = array(
            "title" => "答题用户列表",
            "time"  => time(),
            "data"  => $answerers,
            "get"  => $get,
        );
        $page = $answerers->render();
        $this->assign("data", $tvar);
        $this->assign('page', $page);
        return $this->fetch('exam/answererlist');
    }

    /**
     * @route('exams/useranswers')
     * 查看问卷答题人答题信息
     */
    public function useranswers()
    {
        $get['paper_id'] = input('get.paper_id');
        $get['user_id'] = input('get.user_id');

        $examModel = new ExamModel();
        $exam = $examModel->where('paper_id','=',$get['paper_id'])->find();

        if(empty($exam)){
            $this->error('不存在该问卷');
        }
        $answerModel = new ExamAnswer();
        $answers = $answerModel->get_answerer_info(['paper_id' => $get['paper_id'],'user_id' => $get['user_id']] ,'u.user_id,s.*','s.add_time desc','',$this->limit);
//        echo "<pre>";
//        var_dump($answerers->toArray());die;
        $tvar = array(
            "title" => "用户答题记录",
            "time"  => time(),
            "data"  => $answers,
            "get"  => $get,
        );
        $page = $answers->render();
        $this->assign("data", $tvar);
        $this->assign('page', $page);
        return $this->fetch('exam/useranswers');
    }
}